<template>
  <div class="login_container">
    <div class="mauto">
      <div class="title">扣丁狼电商管理系统</div>
      <div class="centerbox">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="pass">
            <el-input
              type="text"
              placeholder=" 帐号"
              v-model="ruleForm.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input
              placeholder=" 密码"
              type="password"
              v-model="ruleForm.pwd"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <div class="round">
            <el-form-item>
              <el-button
                round
                style="width: 100%"
                type="primary"
                @click="loginFn()"
                >登录</el-button
              >
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
 
<script>
import { LoginApi } from "../../request/api";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          { validator: this.loginVali, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 点击按钮触发登录
    loginFn() {
      this.$refs["ruleForm"].validate((vali) => {
        if (vali) {
          console.log("通过");
          LoginApi({
            password: this.ruleForm.pwd,
            username: this.ruleForm.username,
          }).then((res) => {
            if (res.errno === 0) {
              localStorage.setItem("token", res.data.token);
              this.$router.push("homepage");
            }
          });
        } else {
          console.log("不通过");
        }
      });
    },

    loginVali(rule, value, cb) {
      console.log(value);
      if (value.length > 1 && value.lengtt < 20) {
        cb();
      } else {
        cb("账号长度为1到20个字符");
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #324057;

  .mauto {
    width: 330px;
    height: 210px;
    color: #fff;
    text-align: center;
    position: absolute;
    padding-top: 10px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    font-size: 24px;
    .title {
      height: 51px;
    }
    .centerbox {
      border-radius: 5px;
    }
  }
}
</style>